package cn.iocoder.yudao.module.system.controller.admin.workappendage;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.workappendage.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workappendage.WorkAppendageDO;
import cn.iocoder.yudao.module.system.service.workappendage.WorkAppendageService;

@Tag(name = "管理后台 - 工单附属物")
@RestController
@RequestMapping("/system/work-appendage")
@Validated
public class WorkAppendageController {

    @Resource
    private WorkAppendageService workAppendageService;

    @PostMapping("/create")
    @Operation(summary = "创建工单附属物")
    @PreAuthorize("@ss.hasPermission('system:work-appendage:create')")
    public CommonResult<Long> createWorkAppendage(@Valid @RequestBody WorkAppendageSaveReqVO createReqVO) {
        return success(workAppendageService.createWorkAppendage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单附属物")
    @PreAuthorize("@ss.hasPermission('system:work-appendage:update')")
    public CommonResult<Boolean> updateWorkAppendage(@Valid @RequestBody WorkAppendageSaveReqVO updateReqVO) {
        workAppendageService.updateWorkAppendage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单附属物")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:work-appendage:delete')")
    public CommonResult<Boolean> deleteWorkAppendage(@RequestParam("id") Long id) {
        workAppendageService.deleteWorkAppendage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除工单附属物")
                @PreAuthorize("@ss.hasPermission('system:work-appendage:delete')")
    public CommonResult<Boolean> deleteWorkAppendageList(@RequestParam("ids") List<Long> ids) {
        workAppendageService.deleteWorkAppendageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单附属物")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:work-appendage:query')")
    public CommonResult<WorkAppendageRespVO> getWorkAppendage(@RequestParam("id") Long id) {
        WorkAppendageDO workAppendage = workAppendageService.getWorkAppendage(id);
        return success(BeanUtils.toBean(workAppendage, WorkAppendageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单附属物分页")
    @PreAuthorize("@ss.hasPermission('system:work-appendage:query')")
    public CommonResult<PageResult<WorkAppendageRespVO>> getWorkAppendagePage(@Valid WorkAppendagePageReqVO pageReqVO) {
        PageResult<WorkAppendageDO> pageResult = workAppendageService.getWorkAppendagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkAppendageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单附属物 Excel")
    @PreAuthorize("@ss.hasPermission('system:work-appendage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkAppendageExcel(@Valid WorkAppendagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkAppendageDO> list = workAppendageService.getWorkAppendagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单附属物.xls", "数据", WorkAppendageRespVO.class,
                        BeanUtils.toBean(list, WorkAppendageRespVO.class));
    }

}